unit pagesform;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, BCLabel, BCButton, Forms, Controls, Graphics,
  Dialogs, ExtCtrls, StdCtrls, ComCtrls;

type

  { TfrmPages }

  TfrmPages = class(TForm)
    BCButton2: TBCButton;
    BCButton3: TBCButton;
    BCButton4: TBCButton;
    BCButton5: TBCButton;
    BCLabel1: TBCLabel;
    Image1: TImage;
    Label1: TLabel;
    Label2: TLabel;
    ls: TListView;
    Panel1: TPanel;
    Panel2: TPanel;
    procedure BCButton2Click(Sender: TObject);
    procedure BCButton3Click(Sender: TObject);
    procedure BCButton4Click(Sender: TObject);
    procedure BCButton5Click(Sender: TObject);
    procedure BCLabel1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
      );
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure lsDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure lsDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
  private
    Moving:boolean;
    XPos,YPos:integer;
  public

  end;

var
  frmPages: TfrmPages;

implementation

uses
  datamodule, editpageform;

{$R *.frm}

{ TfrmPages }


procedure TfrmPages.BCLabel1Click(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

procedure TfrmPages.BCButton3Click(Sender: TObject);
var
  mItem:TListItem;
  i:integer;
begin
  //弹出新增对话窗
  frmEditPage:=TfrmEditPage.Create(Application);
  frmEditPage.Label1.Caption:='新增自定义页面';
  //生成序列号
  frmEditPage.Spin1.MinValue:=0;
  frmEditPage.Spin1.MaxValue:=ls.Items.Count;
  frmEditPage.Spin1.Value:=ls.Items.Count;
  frmEditPage.Edit1.Text:='';
  frmEditPage.Edit2.Text:='';
  if frmEditPage.ShowModal=mrOK then
  begin
    //添加到列表中
    if frmEditPage.Spin1.Value<ls.items.Count then
    begin
      //插入
      mItem:=ls.Items.Insert(frmEditPage.Spin1.Value);
      //整理其后的序号
      for i:=frmEditPage.Spin1.Value to ls.Items.Count-1 do
      begin
        ls.Items.Item[i].Caption:=inttostr(i);
      end;
    end
    else
    begin
      //追加
      mItem:=ls.Items.Add;
    end;
    with mItem do
    begin
      Caption:=inttostr(frmEditPage.Spin1.Value);
      SubItems.Add(Trim(frmEditPage.Edit1.Text));
      SubItems.Add(Trim(frmEditPage.Edit2.Text));
      SubItems.Add('a');
    end;
  end;
  frmEditPage.Free;
  frmEditPage:=nil;
end;

procedure TfrmPages.BCButton2Click(Sender: TObject);
var
  i:integer;
begin
  //保存到数据库
  with dm.pubqry do
  begin
    Close;
    Sql.Clear;
    Sql.Add('delete from pages');
    ExecSQL;
    for i:=0 to ls.Items.Count-1 do
    begin
      Close;
      Sql.Clear;
      Sql.Add('insert into pages (id,caption,url) values (:id,:caption,:url)');
      Params.ParamByName('id').AsInteger:=strtoint(ls.Items.Item[i].Caption);
      Params.ParamByName('caption').AsString:=ls.Items.Item[i].SubItems.Strings[0];
      Params.ParamByName('url').AsString:=ls.Items.Item[i].SubItems.Strings[1];
      ExecSQL;
    end;
  end;
  MessageDlg('保存完毕，设置将在下次启动程序时生效。',mtInformation,[mbOK],0);
  Close;
end;

procedure TfrmPages.BCButton4Click(Sender: TObject);
begin
  //删除选中
  if ls.Selected=nil then
  begin
    MessageDlg('请先选中要删除的项目。',mtWarning,[mbOK],0);
    ls.SetFocus;
    Exit;
  end;
  if Messagedlg('确认要删除选中的项目吗？',mtConfirmation,[mbYEs,mbNo],0)=mrYEs then
  begin
    ls.Selected.Delete;
  end;
end;

procedure TfrmPages.BCButton5Click(Sender: TObject);
begin
  //编辑选中
  if ls.Selected=nil then
  begin
    MessageDlg('请先选中要编辑的项目。',mtWarning,[mbOK],0);
    ls.SetFocus;
    Exit;
  end;
  //弹出编辑框
  frmEditPage:=TfrmEditPage.Create(Application);
  frmEditPage.Label1.Caption:='编辑自定义页面';
  frmEditPage.Spin1.Value:=strtoint(ls.Selected.Caption);
  frmEditPage.Spin1.Enabled:=False;
  frmEditPage.Edit1.Text:=ls.Selected.SubItems.Strings[0];
  frmEditPage.Edit2.Text:=ls.Selected.SubItems.Strings[1];
  if frmEditPage.ShowModal=mrOK then
  begin
    ls.Selected.SubItems.Strings[0]:=Trim(frmEditPage.Edit1.Text);
    ls.Selected.SubItems.Strings[1]:=Trim(frmEditPage.Edit2.Text);
    ls.Selected.SubItems.Strings[2]:='u';
  end;
  frmEditPage.Free;
  frmEditPage:=nil;
end;

procedure TfrmPages.FormShow(Sender: TObject);
var
  i:integer;
begin
  //填充到LS
  ls.Items.BeginUpdate;
  ls.Items.Clear;
  for i:=0 to Length(dm.Pages)-1 do
  begin
    with ls.Items.Add do
    begin
      caption:=inttostr(i);
      SubItems.Add(dm.Pages[i].caption);
      SubItems.Add(dm.Pages[i].url);
      Subitems.Add(inttostr(dm.Pages[i].Flag));
    end;
  end;
  ls.Items.EndUpdate;
end;

procedure TfrmPages.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if mbLeft = Button then
  begin
    Self.Moving:=True;
    Self.YPos := Y;
    Self.XPos := X;
  end;
end;

procedure TfrmPages.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if not (csDesigning in Self.ComponentState) then
  begin
    if Self.Moving then
    begin
      Self.Top:=Self.Top+Y-Self.YPos;
      Self.Left:=Self.Left+X-Self.XPos;
      if Self.Top<0 then
        Self.Top:=0;
    end;
  end;
end;

procedure TfrmPages.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Moving:=False;
end;

procedure TfrmPages.lsDragDrop(Sender, Source: TObject; X, Y: Integer);
var
  ms,mt:TListItem;
  i:integer;
begin
  ls.Items.BeginUpdate;
  //将放下的项目插入到目标项目的前面
  ms:=ls.Selected;
  mt:=ls.GetItemAt(X,Y);
  ls.Items.Move(ms.Index,mt.Index);
  //重整序号
  for i:=0 to ls.Items.Count-1 do
  begin
    ls.Items.Item[i].Caption:=inttostr(i);
  end;
  ls.Items.EndUpdate;
end;

procedure TfrmPages.lsDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var
  ms,mt:TListItem;
begin
  //如果不是被选项自己则接受
  if Source=ls then
  begin
    ms:=ls.Selected;
    mt:=ls.GetItemAt(X,Y);
    if mt<>nil then
    begin
      if mt<>ms then
        Accept:=True
      else
        Accept:=False;
    end
    else
      Accept:=False;
  end
  else
    Accept:=False;
end;

end.

